package com.jincp.common.networkstate;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import static android.content.Context.WIFI_SERVICE;

/**
 * Author : zhousf
 */
public class NetworkUtil {

    /**
     * 网络是否可用
     */
    public static boolean isNetworkAvailable(Context context){
        if (context != null) {
            ConnectivityManager cm = (ConnectivityManager)
                    context.getSystemService(Context.CONNECTIVITY_SERVICE);
            final NetworkInfo network = cm.getActiveNetworkInfo();
            if(network != null && network.getState() == NetworkInfo.State.CONNECTED){
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否有网络连接
     */
    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo network = mConnectivityManager
                    .getActiveNetworkInfo();
            if (network != null) {
                return network.isAvailable();
            }
        }
        return false;
    }

    /**
     * 判断WIFI网络是否可用
     */
    public static boolean isWifiConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo wifi = mConnectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (wifi != null) {
                return wifi.isAvailable();
            }
        }
        return false;
    }

    /**
     * 判断MOBILE网络是否可用
     */
    public static boolean isMobileConnected(Context context){
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mobile = mConnectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (mobile != null) {
                return mobile.isAvailable();
            }
        }
        return false;
    }

    /**
     * 获取当前网络连接的类型信息
     */
    public static int getNetworkTypeConnected(Context context){
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo network = mConnectivityManager
                    .getActiveNetworkInfo();
            if (network != null && network.isAvailable()) {
                return network.getType();
            }
        }
        return -1;
    }

    /**
     *
     *获取当前的网络状态
     */
    public static String getNetworkType(Context context){
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo network = cm.getActiveNetworkInfo();
        if (network == null) {
            return NetInfo.NONE_NET;
        }
        if (network.getType() == ConnectivityManager.TYPE_MOBILE) {
            return NetInfo.CM_NET;
        } else if (network.getType() == ConnectivityManager.TYPE_WIFI) {
            return NetInfo.WIFI;
        }
        return NetInfo.NONE_NET;

    }

    /**
     * 获取本机WIFI
     *
     * @return
     */
    public static String getLocalIpAddress(Context context) {
        String ip = "127.0.0.1";
        int netWorkConnectionType = getNetworkTypeConnected(context);
        switch (netWorkConnectionType) {
            case ConnectivityManager.TYPE_WIFI:
                ip = getWifiIpAddress(context);
                break;
            case ConnectivityManager.TYPE_MOBILE:
                ip = get3GIpAddress(context);
                break;
            default:
                break;
        }
        return ip;
    }

    /**
     * 3G网络IP
     *
     * @return
     */
    public static String getWifiIpAddress(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        // 获取32位整型IP地址
        int ipAddress = wifiInfo.getIpAddress();
        //返回整型地址转换成“*.*.*.*”地址
        return String.format("%d.%d.%d.%d",
                (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
                (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
    }

    /**
     * 3G网络IP
     *
     * @return
     */
    public static String get3GIpAddress(Context context) {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddress = intf
                        .getInetAddresses(); enumIpAddress.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddress.nextElement();
                    if (!inetAddress.isLoopbackAddress()
                            && inetAddress instanceof Inet4Address) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
